package cn.edu.fzu.delivery.controller.delivery;

import cn.edu.fzu.delivery.common.PageVO;
import cn.edu.fzu.delivery.common.Result;
import cn.edu.fzu.delivery.common.RetCode;
import cn.edu.fzu.delivery.domain.entity.Courier;
import cn.edu.fzu.delivery.domain.entity.CourierDelivery;
import cn.edu.fzu.delivery.domain.entity.SysUser;
import cn.edu.fzu.delivery.domain.enums.*;
import cn.edu.fzu.delivery.service.CourierService;
import cn.edu.fzu.delivery.service.DeliveryManService;
import cn.edu.fzu.delivery.service.LogisticsService;
import cn.edu.fzu.delivery.session.SessionConstant;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.oas.annotations.EnableOpenApi;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 快递员快递管理
 * 
 * @author jiebin
 * @data 2022/11/9
 **/
@RestController
@RequestMapping("/api/auth/deliveryman/courier/")
@EnableOpenApi
@Api(tags = "快递员-快递接口")
public class DeliveryManCourierController {

    @Resource
    private DeliveryManService deliveryManService;
    @Resource
    private CourierService     courierService;

    @PostMapping("/updateDeliveryStatus")
    @ApiOperation(value = "快递员：更新派送状态", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "courierId", required = true),
            @ApiImplicitParam(name = "status", value = "-1-派送失败 0-等待派送 1-派送中 2-派送成功", required = true) })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result updateCourierStatus(String courierId, DeliveryStatusEnum status, HttpServletRequest request) {
        SysUser user = (SysUser) request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);
        deliveryManService.updateDeliveryStatus(user, courierId, status);
        return new Result(RetCode.SUCCESS, "更新快递信息成功");
    }


    @ApiOperation(value = "快递员：查询快递详情", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "courierId", value = "快递单号") })
    @RequestMapping("/detail")
    public Result detail(String courierId) {
        //这里就不做权限校验，只要有快递单号，不管是不是自己负责的快递都可以查询
        return new Result(courierService.getByCourierId(courierId));
    }


    @GetMapping("/listCouriers")
    @ApiOperation(value = "快递员：获取快递列表", httpMethod = "GET", notes = "")
    @ApiImplicitParams({ @ApiImplicitParam(name = "status", value = "状态 -1-派送失败 0-等待派送 1-派送中 2-派送成功"),
            @ApiImplicitParam(name = "startTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间") })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result listCouriers(String courierId, DeliveryStatusEnum status, String startTime, String endTime,
                               Integer pageNum, Integer pageSize, HttpServletRequest request) {
        SysUser user = (SysUser) request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);
        try (Page<CourierDelivery> page = PageHelper.startPage(pageNum, pageSize)) {
            deliveryManService.listCouriers(user.getId(), courierId, status, startTime, endTime);
            return new Result(new PageVO<>(page.getTotal(), page.getResult()));
        }
    }


    @ApiOperation(value = "快递员：完善顾客寄快递的信息-重量", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "courierId", value = "快递单号"),
            @ApiImplicitParam(name = "weight", value = "重量") })
    @PostMapping("/updateCourier")
    public Result updateCourier(String courierId, Double weight, HttpServletRequest request) {
        SysUser user = (SysUser) request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);
        deliveryManService.updateCourier(user, courierId, weight);
        return Result.SUCCESS;
    }
}
